package me.zhengjie.modules;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import me.zhengjie.config.AppConfig;
import me.zhengjie.modules.utils.LogUtils;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class ResponseAOP {

    @AfterReturning(returning = "ret", pointcut = "execution(* me.zhengjie.modules.biz.rest.*.*(..))")
    public void doAfterReturning(Object ret) {
        if (!AppConfig.isDebug) return;

        System.out.println("返回值 : ");
        System.out.println("");
        System.out.println(JSON.toJSONString(ret));
        System.out.println("");

        if (AppConfig.showResult) {
            if (AppConfig.showResultPretty) {
                LogUtils.writeToFile(
                        JSON.toJSONString(ret, SerializerFeature.PrettyFormat,
                                SerializerFeature.WriteMapNullValue,
                                SerializerFeature.WriteDateUseDateFormat
                        )
                );
            } else {
                LogUtils.writeToFile(JSON.toJSONString(ret));
            }
        }
    }

    @AfterReturning(returning = "ret", pointcut = "execution(* me.zhengjie.modules.system.rest.*.*(..))")
    public void doSystemAfterReturning(Object ret) {
        if (!AppConfig.isDebug) return;

        System.out.println("返回值 : ");
        System.out.println("");
        System.out.println(JSON.toJSONString(ret));
        System.out.println("");

        if (AppConfig.showResult) {
            if (AppConfig.showResultPretty) {
                LogUtils.writeToFile(
                        JSON.toJSONString(ret, SerializerFeature.PrettyFormat,
                                SerializerFeature.WriteMapNullValue,
                                SerializerFeature.WriteDateUseDateFormat
                        )
                );
            } else {
                LogUtils.writeToFile(JSON.toJSONString(ret));
            }
        }

    }
}
